/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.VesselDAO;
import exatrez.dao.VesselGroupDAO;
import exatrez.model.Vessel;
import exatrez.model.VesselGroup;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author wan.fairul
 */
public class VesselManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private VesselDAO vesselDAO;
    @Inject
    private VesselGroupDAO vesselGroupDAO;

    @Transactional
    public void saveOrUpdate(Vessel vessel) {
        vesselDAO.saveOrUpdate(vessel);
    }

    @Transactional
    public void deleteVessel(Vessel vessel) {
        vesselDAO.delete(vessel);
    }

    public Vessel findVesselNameAndAbbr(String vesselName, String vesselAbbr) {
        String query = "SELECT a FROM Vessel a WHERE a.vesselName = :vesselName and a.vesselAbbr = :vesselAbbr ";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("vesselName", vesselName);
        q.setString("vesselAbbr", vesselAbbr);
        return (Vessel) q.uniqueResult();
    }
    
     public Vessel findVesselName(String vesselName) {
        String query = "SELECT a FROM Vessel a WHERE a.vesselName = :vesselName";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("vesselName", vesselName);
        return (Vessel) q.uniqueResult();
    }
     
      public Vessel findVesselAbbr(String vesselId, String vesselAbbr) {
        String query = "SELECT a FROM Vessel a WHERE a.vesselId != :vesselId and a.vesselAbbr = :vesselAbbr ";
        Query q = sessionProvider.get().createQuery(query);
         q.setString("vesselId", vesselId);
        q.setString("vesselAbbr", vesselAbbr);
        return (Vessel) q.uniqueResult();
    }

    public List<Vessel> findAll() {
        String query = "SELECT a FROM Vessel a WHERE a.vesselId is not null  ";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    @Transactional
    public void saveOrUpdateVesselGroup(VesselGroup vesselGroup) {
        vesselGroupDAO.saveOrUpdate(vesselGroup);
    }

    @Transactional
    public void deleteVesselGroup(VesselGroup vesselGroup) {
        vesselGroupDAO.delete(vesselGroup);
    }

    public List<VesselGroup> findAllVesselGroup() {
        String query = "SELECT a FROM VesselGroup a WHERE a.vesselGroupId is not null  ";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    public VesselGroup findVesselUser(String vesselId, String userId) {
        String query = "SELECT a FROM VesselGroup a WHERE a.vessel.vesselId = :vesselId and a.user.userId = :userId";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("vesselId", vesselId);
        q.setString("userId", userId);
        return (VesselGroup) q.uniqueResult();
    }

    public VesselDAO getVesselDAO() {
        return vesselDAO;
    }

    public void setVesselDAO(VesselDAO vesselDAO) {
        this.vesselDAO = vesselDAO;
    }

    public VesselGroupDAO getVesselGroupDAO() {
        return vesselGroupDAO;
    }

    public void setVesselGroupDAO(VesselGroupDAO vesselGroupDAO) {
        this.vesselGroupDAO = vesselGroupDAO;
    }
}
